package org.example;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @FileName Test
 * @Description
 * @Author coderfun
 * @selfDesc coding in fun
 * @date 2024-11-25
 **/
public class Test {
    public static void main(String[] args) {
        System.out.println(compressString("aaabbcdddd"));
    }
    public static String compressString(String inputStr) {
        if(inputStr == null || inputStr.equals("")){
            return "";
        }
        if(inputStr.length()==1){
            return inputStr;
        }
        // 在这⾥写代码
        int oldlen = inputStr.length();
        HashMap<Character,Integer> cnt = new LinkedHashMap<>();
        StringBuffer res = new StringBuffer("");
        char[] req = inputStr.toCharArray();
        for(int i = 0;i<oldlen;i++){
            if(cnt.containsKey(req[i])){
                int cur = cnt.get(req[i]);
                cnt.put(req[i],cur+1);
            }else{
                cnt.put(req[i],1);
            }
        }
        for (Map.Entry<Character, Integer> entry : cnt.entrySet()) {
            res.append(entry.getKey());
            res.append(entry.getValue());
        }
        int newlen = res.length();
        System.out.println(inputStr);
        System.out.println(res.toString());
        if(newlen>oldlen){
            return inputStr;
        }else{
            return res.toString();
        }
    }
}
